﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace WXTao
{
    class DaTaoKe
    {
        /// <summary>
        /// 时间戳计时开始时间
        /// </summary>
        private static DateTime timeStampStartTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        private string app_secret = "7691ab2a6e0ab8f8540a4d2986222617";
        private string app_key = "5dce04a551291";

        public DaTaoKe()
        {
            
        }

        public string getData(string url, string param)
        {
            string api_url = url.EndsWith(@"?") ? url.Trim() : url.Trim() + @"?";
            string api_params = param.Trim();

            Random rd = new Random();
            int nonce = rd.Next(100000, 1000000);
            long timer = (long)(DateTime.Now - timeStampStartTime).TotalMilliseconds;

            string signparam = "appKey=" + app_key + "&timer=" + timer + "&nonce=" + nonce;
            api_url += signparam + param+"&signRan=" + makeSign(signparam);
            try
            {
                using (var client = new HttpClient())
                {
                    var responseString = client.GetStringAsync(api_url);
                    return responseString.Result;
                }
            }
            catch(Exception e)
            {
                return e.Message.ToString();
            }
        }


        string makeSign(string param)
        {
            string rst = param + "&key=" +app_secret;
            return MD5Helper.Md5(rst).ToUpper();
        }

        
        //每日爆品推荐
        public string getExplosive_goods_list()
        {
            string url = "https://openapi.dataoke.com/api/goods/explosive-goods-list";
            string param = "&version=v1.0.0&pageId=1&pageSize=10";
            return getData(url, param);
        }

        //创建淘口令
        public string creat_taokouling(string t,string tb_url)
        {
            string text = t;
            string jump_url = System.Web.HttpUtility.UrlEncode(tb_url);
            string url = "https://openapi.dataoke.com/api/tb-service/creat-taokouling";
            string param = "&version=v1.0.0&text=" + text+"&url="+ jump_url;
            return getData(url,  param);
        }


        public string parse_taokouling(string content)
        {
            string url = "https://openapi.dataoke.com/api/tb-service/parse-taokouling";
            string param = "&version=v1.0.0&content=" + content;
            return getData(url, param);
        }

        public string pyq_wenan()
        {
            string url = "https://openapi.dataoke.com/api/goods/friends-circle-list";
            string param = "&version=v1.3.0&pageId=1&pageSize=100";
            return getData(url, param);
            
        }

        public string get_privilege_link(string goodsId)
        {
            string url = "https://openapi.dataoke.com/api/tb-service/get-privilege-link";
            string param = "&version=v1.3.0&goodsId=" + goodsId;
            return getData(url, param);
        }

        public TBKData[] getGoods(int num,int cid)
        {
            //string url = "https://openapi.dataoke.com/api/goods/explosive-goods-list";
            //string param = "&version=v1.0.0&pageId=1&pageSize=50&PriceCid="+cid;
            string url = "https://openapi.dataoke.com/api/goods/pull-goods-by-time";
            string param = "&version=v1.2.3&pageId=1&pageSize=10&sort=1";
            List<TBKData> tbkList = new List<TBKData>();
            for (int i = 0; i < num; i++)
            {
                TBKData tbk = new TBKData();
                //Random ran = new Random();
                //int RandKey = ran.Next(0, 19);
                int RandKey = 0;
                string data = getData(url, param);
                try
                {
                    JObject jObj = JObject.Parse(data);
                    if (jObj["code"] != null && jObj["code"].ToString().Equals("0"))
                    {
                        JObject data_obj = JObject.Parse(jObj["data"].ToString());
                        JArray list_array = JArray.Parse(data_obj["list"].ToString());

                        tbk.Wenan = System.Web.HttpUtility.UrlDecode(list_array[RandKey]["desc"].ToString());
                        tbk.GoodsId = list_array[RandKey]["goodsId"].ToString();
                        tbk.MarketingMainPic = list_array[RandKey]["marketingMainPic"].ToString();
                        if (tbk.MarketingMainPic.Equals(""))
                        {
                            tbk.MarketingMainPic = list_array[RandKey]["mainPic"].ToString();
                        }
                        tbk.OriginalPrice = list_array[RandKey]["originalPrice"].ToString();
                        tbk.ActualPrice = list_array[RandKey]["actualPrice"].ToString();
                        tbk.CouponPrice = list_array[RandKey]["couponPrice"].ToString();
                        tbk.Dtitle = list_array[RandKey]["dtitle"].ToString();
                    }
                    else
                    {
                        if (jObj["msg"] != null)
                            tbk.ErrorMsg = jObj["msg"].ToString();
                        else if (jObj["code"] != null)
                            tbk.ErrorMsg = "ERROR Code:" + jObj["code"].ToString();
                        else
                            tbk.ErrorMsg = "ERROR " + data;
                        tbkList.Add(tbk);
                        continue;
                    }

                    data = get_privilege_link(tbk.GoodsId);

                    jObj = JObject.Parse(data);
                    if (jObj["code"] != null && jObj["code"].ToString().Equals("0"))
                    {
                        JObject data_obj = JObject.Parse(jObj["data"].ToString());

                        tbk.CouponClickUrl = data_obj["couponClickUrl"].ToString();
                        tbk.Tpwd = data_obj["tpwd"].ToString();
                        tbk.CouponInfo = data_obj["couponInfo"].ToString();
                    }
                    else
                    {
                        if (jObj["msg"] != null)
                            tbk.ErrorMsg = jObj["msg"].ToString();
                        else if (jObj["code"] != null)
                            tbk.ErrorMsg = "ERROR Code:" + jObj["code"].ToString();
                        else
                            tbk.ErrorMsg = "ERROR " + data;
                        tbkList.Add(tbk);
                        continue;
                    }
                    tbk.ErrorMsg = "";
                    tbkList.Add(tbk);
                    continue;
                }
                catch (Exception e)
                {
                    tbk.ErrorMsg = "data:" + data + " ERROR:" + e.Message.ToString();
                    tbkList.Add(tbk);
                    continue;
                }
            }

            return tbkList.ToArray();
        }

        public TBKData[] getSuggest(int num)
        {
            List<TBKData> tbkList = new List<TBKData>();
            for (int i=0;i< num; i++)
            {
                TBKData tbk = new TBKData();
                Random ran = new Random();
                int RandKey = ran.Next(0, 99);
                //int RandKey = 0;
                string data = pyq_wenan();
                try
                {
                    JObject jObj = JObject.Parse(data);
                    if (jObj["code"] != null && jObj["code"].ToString().Equals("0"))
                    {
                        JObject data_obj = JObject.Parse(jObj["data"].ToString());
                        JArray list_array = JArray.Parse(data_obj["list"].ToString());

                        tbk.Wenan = System.Web.HttpUtility.UrlDecode(list_array[RandKey]["circleText"].ToString());
                        tbk.GoodsId = list_array[RandKey]["goodsId"].ToString();
                        tbk.MarketingMainPic = list_array[RandKey]["marketingMainPic"].ToString();
                        if (tbk.MarketingMainPic.Equals(""))
                        {
                            tbk.MarketingMainPic = list_array[RandKey]["mainPic"].ToString();
                        }
                        tbk.OriginalPrice = list_array[RandKey]["originalPrice"].ToString();
                        tbk.ActualPrice = list_array[RandKey]["actualPrice"].ToString();
                        tbk.CouponPrice = list_array[RandKey]["couponPrice"].ToString();
                        tbk.Dtitle = list_array[RandKey]["dtitle"].ToString();
                    }
                    else
                    {
                        if (jObj["msg"] != null)
                            tbk.ErrorMsg = jObj["msg"].ToString();
                        else if (jObj["code"] != null)
                            tbk.ErrorMsg = "ERROR Code:" + jObj["code"].ToString();
                        else
                            tbk.ErrorMsg = "ERROR " + data;
                        tbkList.Add(tbk);
                        continue;
                    }

                    data = get_privilege_link(tbk.GoodsId);

                    jObj = JObject.Parse(data);
                    if (jObj["code"] != null && jObj["code"].ToString().Equals("0"))
                    {
                        JObject data_obj = JObject.Parse(jObj["data"].ToString());

                        tbk.CouponClickUrl = data_obj["couponClickUrl"].ToString();
                        tbk.Tpwd = data_obj["tpwd"].ToString();
                        tbk.CouponInfo = data_obj["couponInfo"].ToString();
                    }
                    else
                    {
                        if (jObj["msg"] != null)
                            tbk.ErrorMsg = jObj["msg"].ToString();
                        else if (jObj["code"] != null)
                            tbk.ErrorMsg = "ERROR Code:" + jObj["code"].ToString();
                        else
                            tbk.ErrorMsg = "ERROR " + data;
                        tbkList.Add(tbk);
                        continue;
                    }
                    tbk.ErrorMsg = "";
                    tbkList.Add(tbk);
                    continue;
                }
                catch (Exception e)
                {
                    tbk.ErrorMsg = "data:" + data + " ERROR:" + e.Message.ToString();
                    tbkList.Add(tbk);
                    continue;
                }
            }

            return tbkList.ToArray();
        }
    }


}
